package jt.classic.app.dms.timer;

import java.io.File;
import java.sql.Connection;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;


/**
 * DMS 用户空间文件夹初始化定时扫描器
 *
 * @author mido
 *
 */
public class DMSUserFolderTimer {
    private Timer timer = new Timer();
    private long delay = 1000;
    private long period = 10000;
    private File root;

    public DMSUserFolderTimer(File folder) {
        this.root = folder;
    }

    public void runTask() {
        System.out.println(
            "system message: DMS user-folder timer task is running...");
        timer.schedule(new MyTask(this.root), this.delay, this.period);
    }

    public void stopTask() {
        System.out.println(
            "system message: DMS user-folder timer task is cancelled...");
        timer.cancel();
    }

    private class MyTask extends TimerTask {
        private File root;

        public MyTask(File folder) {
            this.root = folder;
        }

        public void run() {
            if (this.root.exists() && this.root.isDirectory()) {
                Connection conn = null;

                SystemDataSource source = SystemDataSource.getInstance();

                try {
                    conn = source.getConnection();

                    UserManager manager = new UserManager(conn);

                    List list = manager.getAllUser();

                    if ((list != null) && !list.isEmpty()) {
                        for (int i = 0; i < list.size(); i++) {
                            IUser user = (IUser) list.get(i);

                            /*用户空间*/
                            File userFolder = new File(this.root, user.ilogid());

                            if (!userFolder.exists()) {
                                /*生成用户空间*/
                                userFolder.mkdir();
                            }

                            /*用户下载文件所在文件夹*/
                            File downloadFolder = new File(userFolder,
                                    "downloads");

                            if (!downloadFolder.exists()) {
                                downloadFolder.mkdir();
                            }

                            /*用户通过ftp上传的文件所在文件夹*/
                            File uploadFolder = new File(userFolder, "uploads");

                            if (!uploadFolder.exists()) {
                                uploadFolder.mkdir();
                            }
                        }
                    }
                } catch (Exception ex) {
                    ISystem.catchException("DMS initialize user-home error.", ex);
                } finally {
                    SystemDataSource.closeConnection(conn);
                }
            }
        }
    }
}
